package nullInvader.Engine;

import java.awt.Point;
import java.io.IOException;

import javax.imageio.ImageIO;

/**
 * Applikationsutveckling i Java, 5DV135 HT12 Project
 * 
 * File: UnitTeleporter.java Description: A unit that has the ability to place
 * teleport bricks
 * 
 * @see Teleport
 * 
 *      Date: 17 december 2012
 * 
 *      cs-user: dv11flh
 * @author Fredrik Loch
 */
public class UnitTeleporter extends Unit {

    protected Point startPoint;
    protected Point endPoint;
    protected Boolean placed;

    public UnitTeleporter(Point startPos, Player p, int price) {
	super(startPos, p, price);
	this.health = 10;
	this.clickable = true;
	startPoint = null;
	endPoint = null;
	placed = false;
	setIcon("/nullInvader/Resources/Unit2.png");
    }

    /*
     * (non-Javadoc)
     * 
     * @see Unit#interact()
     */
    @Override
    public void interact() {
	if (startPoint != null) {
	    endPoint = (Point) this.position.clone();
	    setTeleport();
	} else {
	    startPoint = (Point) this.position.clone();
	    setIcon("/nullInvader/Resources/Unit2T.png");
	}
    }

    /**
     * Method to activate the teleports at the startPoint and the endPoint
     * 
     * @see Teleport
     */
    private void setTeleport() {
	if (!placed) {
	    Bricks b = new Teleport(true, endPoint);
	    p.getCurrentZone(startPoint).setBrick(b);
	    Bricks c = new Teleport(true, endPoint);
	    p.getCurrentZone(endPoint).setBrick(c);
	    placed = true;
	}
    }

    protected void setIcon(String url) {
	try {
	    icon = ImageIO.read(getClass().getResource(url));
	} catch (IOException e) {
	    System.out.println(url + " doesn't exist");
	}
    }

    @Override
    protected void setIcon() {
    }
}
